package com.ruoyi.web.controller.statistics;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.statistics.domain.StatisticsPayExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statistics.domain.StatisticsPay;
import com.ruoyi.statistics.service.IStatisticsPayService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Controller
@RequestMapping("/statistics/statisticsPay")
public class StatisticsPayController extends BaseController
{
    private String prefix = "statistics/statisticsPay";
	
	@Autowired
	private IStatisticsPayService statisticsPayService;
	
	@RequiresPermissions("statistics:statisticsPay:view")
	@GetMapping()
	public String statisticsPay()
	{
	    return prefix + "/statisticsPay";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("statistics:statisticsPay:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(StatisticsPay statisticsPay)
	{
		startPage();
		List<StatisticsPay> list = statisticsPayService.selectStatisticsPayList(statisticsPay);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
	@RequiresPermissions("statistics:statisticsPay:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StatisticsPay statisticsPay)
    {
    	List<StatisticsPay> list = statisticsPayService.selectStatisticsPayList(statisticsPay);
		List<StatisticsPayExport> statisticsPayExports = dataConvert(list);
		ExcelUtil<StatisticsPayExport> util = new ExcelUtil<StatisticsPayExport>(StatisticsPayExport.class);
        return util.exportExcel(statisticsPayExports, "statisticsPay");
    }


	/**
	 * 数据转化
	 */
	public  List<StatisticsPayExport> dataConvert(List<StatisticsPay> list) {
		List<StatisticsPayExport> statisticsPayExports = new ArrayList<>();
		for (StatisticsPay statisticsPay : list) {
			StatisticsPayExport statisticsPayExport = new StatisticsPayExport();
			BeanUtils.copyProperties(statisticsPay, statisticsPayExport);
			BigDecimal yuan = new BigDecimal("100");
			// 预期支付额
			statisticsPayExport.setExpectFee(statisticsPay.getExpectFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 预期渠道入账
			statisticsPayExport.setExpectChFee(statisticsPay.getExpectChFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 预期商户入账
			statisticsPayExport.setExpectMchFee(statisticsPay.getExpectMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 预期财务入账
			statisticsPayExport.setExpectFinFee(statisticsPay.getExpectFinFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际支付额
			statisticsPayExport.setRealFee(statisticsPay.getRealFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际渠道入账
			statisticsPayExport.setRealChFee(statisticsPay.getRealChFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际商户入账
			statisticsPayExport.setRealMchFee(statisticsPay.getRealMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际财务入账
			statisticsPayExport.setRealFinFee(statisticsPay.getRealFinFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 统计类型
			if (statisticsPay.getStatisticsType() == 1) {
				statisticsPayExport.setType("天");
			}else if (statisticsPay.getStatisticsType() == 2) {
				statisticsPayExport.setType("小时");
			}
			statisticsPayExports.add(statisticsPayExport);
		}
		return statisticsPayExports;
	}

	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@RequiresPermissions("statistics:statisticsPay:add")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(StatisticsPay statisticsPay)
	{		
		return toAjax(statisticsPayService.insertStatisticsPay(statisticsPay));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		StatisticsPay statisticsPay = statisticsPayService.selectStatisticsPayById(id);
		mmap.put("statisticsPay", statisticsPay);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("statistics:statisticsPay:edit")
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(StatisticsPay statisticsPay)
	{		
		return toAjax(statisticsPayService.updateStatisticsPay(statisticsPay));
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("statistics:statisticsPay:remove")
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(statisticsPayService.deleteStatisticsPayByIds(ids));
	}
	
}
